//
//  LockController.swift
//  LiteAccount
//
//  Created by 刘伟 on 2017/6/5.
//  Copyright © 2017年 刘伟. All rights reserved.
//

import UIKit
import LocalAuthentication

class LockController: UIViewController {
    

    override func viewDidLoad() {
        super.viewDidLoad()
        prepareUI()
        layoutUI()
    }
    
    fileprivate func prepareUI() {
        self.view.backgroundColor = UIColor.la_app
        self.view.addSubview(appIcon)
        self.view.addSubview(messageLabel)
        self.view.addSubview(lockView)
        self.view.addSubview(forgetCodeBtn)
        self.view.addSubview(useFingerBtn)
        
    }
    
    fileprivate func layoutUI() {
        
        appIcon.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(50)
            make.width.equalTo(appIcon.snp.height)
            make.width.equalTo(100)
        }
        
        messageLabel.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(appIcon.snp.bottom).offset(10)
        }
        
        lockView.snp.makeConstraints { (make) in
            make.top.equalTo(messageLabel.snp.bottom).offset(50)
            make.left.right.equalToSuperview()
            make.height.equalTo(lockView.snp.width)
        }
        
        forgetCodeBtn.snp.makeConstraints { (make) in
            make.left.bottom.equalToSuperview()
            make.height.equalTo(50)
            make.width.equalTo(100)
        }
        
        useFingerBtn.snp.makeConstraints { (make) in
            make.bottom.right.equalToSuperview()
            make.height.equalTo(50)
            make.width.equalTo(100)
        }
        
    }
    
    // MARK: 忘记手势密码，则重新登录
    func forgetCode() {
        self.dismiss(animated: true, completion: nil)
    }
    
    // MARK: 指纹模式解锁
    func fingerMode() {
        let laContext = LAContext()
        var error: NSError? = nil 
        if laContext.canEvaluatePolicy(LAPolicy.deviceOwnerAuthenticationWithBiometrics, error: &error) {
            debugPrint("你可以使用指纹")
            laContext.evaluatePolicy(LAPolicy.deviceOwnerAuthenticationWithBiometrics, localizedReason: "请验证指纹", reply: { (success, error) in
                if success {
                    print("验证成功!")
                } else {
                    print("验证失败!")
                }
            })
        } else {
            debugPrint("你不可以使用指纹")
        }
    }
    
    lazy var appIcon: UIImageView = {
        let object = UIImageView()
        object.backgroundColor = UIColor.la_app
        object.image = UIImage(named: "app_icon_white.png")
        object.contentMode = .scaleToFill
        return object
    }()
    
    // messageLabel
    lazy var messageLabel: UILabel = {
        let object = UILabel()
        object.textColor = .white
        object.font = UIFont.la_Font(ofSize: 12)
        object.textAlignment = .center
        object.text = "请绘制手势密码"
        return object
    }()
    
    fileprivate lazy var lockView: LockView = { [unowned self] in
        let object =  LockView()
        object.backgroundColor = UIColor.la_app
        object.delegate = self
        return object
    }()
    // 切换登录与注册
    lazy var forgetCodeBtn: UIButton = {
        let object = UIButton()
        object.setTitle("忘记手势密码?", for: .normal)
        object.titleLabel?.font = UIFont.la_Font(ofSize: 13)
        object.titleLabel?.textAlignment = .left
        object.setTitleColor(UIColor.white, for: .normal)
        object.addTarget(self, action: #selector(forgetCode), for: .touchUpInside)
        return object
    }()
    
    // 发送邮件按钮
    lazy var useFingerBtn: UIButton = {
        let object = UIButton()
        object.setTitle("指纹解锁", for: .normal)
        object.titleLabel?.font = UIFont.la_Font(ofSize: 13)
        object.titleLabel?.textAlignment = .right
        object.setTitleColor(UIColor.white, for: .normal)
        object.addTarget(self, action: #selector(fingerMode), for: .touchUpInside)
        return object
    }()

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

extension LockController: LockViewDelegate {
    func didFinished(_ isSuccess: Bool, title: String, message: String, password: [Int]) {
        debugPrint(isSuccess, title, message)
        messageLabel.text = message
        //lockView.reset()
    }
}
